﻿package {
	import bin.Camera;
	import bin.Renderer;
	import bin.Textor;
	import bin.loader.FBXLoader;

	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Matrix3D;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.media.Sound;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.fscommand;
	import flash.utils.Timer;

	/**
	 * @author coxosclassic
	 */
	public class Main extends MovieClip {
		
		private var models:Array;
		private var renderer:Renderer;
		
		private var worldMat:Matrix3D;
		private var finalMat:Matrix3D;
		
		private var value:Vector3D;
		private var camera:Camera;
		
		private var objDone:Boolean = false;
		private var dataDone:Boolean = false;
		private var soundDone:Boolean = false;
		
		private var textor:Textor;
		private var data:XML;
		
		private var timer:Timer;
		private var currentSlide:String;
		private var currentDelay:int;
		
		private var timerIntro:Timer;
		
		private var invtroStart:Boolean = false;
		
		private var sfx:Sound;
		
		public function Main():void {
			
			initVars();
			loadFileFBX();
		}
		
		private function initVars():void {
			
			fscommand("allowscale", "false");
			stage.showDefaultContextMenu=false;
			
			worldMat = new Matrix3D();
			finalMat = new Matrix3D();
			value = new Vector3D();
			
			models = new Array();
			renderer = new Renderer();
			renderer.x = stage.stageWidth/2;
			renderer.y = stage.stageHeight/2;
			renderer.addShader("shaders/test.pbj");
			addChild(renderer);
			
			textor = new Textor();
			textor.addEventListener("TEXTOR_DONE", script);
			textor.x = 10;
			textor.y = 210;
			textor.visible = false;
			addChild(textor);
			
			timer = new Timer(1000,0);
			timer.addEventListener(TimerEvent.TIMER, changeTextor);
			
			timerIntro = new Timer(1000,0);
			timerIntro.addEventListener(TimerEvent.TIMER, controlIntro);
		}
		
		private function soundLoaded(e:Event):void {
			
			soundDone = true;
			init();
		}
		
		private function loadFileFBX():void {
			
			var loader:FBXLoader = new FBXLoader();
			loader.addEventListener("FILE_LOADED", fileLoaded);
			loader.loadFile("objs/id05Invtro.fbx");
			
			var dataLoader:URLLoader = new URLLoader();
			dataLoader.addEventListener(Event.COMPLETE, dataLoaded);
			dataLoader.load(new URLRequest("data.xml"));
			
			sfx = new Sound();
			sfx.addEventListener(Event.COMPLETE, soundLoaded);
			sfx.load(new URLRequest("sfx/id10-1.mp3"));
		}
		
		private function fileLoaded(e:Event):void {
			
			models = e.target.models;
			
			var i:uint;
			for(i = 0; i < models.length; i++){
				models[i].init();
			}
			
			objDone = true;
			
			init();
		}
		
		private function dataLoaded(e:Event):void {
			
			dataDone = true;
			data = new XML(e.target.data);
			
			init();
		}
		
		private function init():void {
			
			if(objDone && dataDone && soundDone && !invtroStart) {
				
				sfx.play();
				timerIntro.start();
				SetCamera();
				stage.addEventListener(Event.ENTER_FRAME, update);
			}
			
			if(invtroStart){
				SetLayout();
//				stage.addEventListener(Event.ENTER_FRAME, update);
				
			}
		}
		
		private function SetCamera():void {
			
			camera = new Camera();
			camera.setPerspective(10,400,new Point(0,0),new Vector3D(0,0,9));
			camera.rotate(180, 0, 1, 0);
			camera.translate(0, 0, -9);
			renderer.projectionMatrix = camera.toMatrix3D;
		}
		
		private function SetLayout():void {
			
			textor.init(new XML(data.slide1));
			currentSlide = "SLIDE1";
			currentDelay = int(data.slide1.@delay);
		}
		
		private function update(e:Event):void {
			
			value.x+=1;
			value.y+=0.01;
			value.z+=0.5;
			
			renderer.projectionMatrix = camera.toMatrix3D;
			
			if(!invtroStart){
				
				camera.translate(0, 0, value.z/2000);
				//camera.translate(0, -value.y/175, 0);
			}
			else {
				
				renderer.rotationVector = value;
				renderer.translationVector = new Vector3D(0,value.y,0);
				//camera.translate(0, 0, Math.sin(value.z/2000));
			}
				
			renderer.update(models);
			
			sortModels();
			//renderer.filters = [renderer.filter];
		}
		
		private function sortModels():void {
			
			var i:uint;
			for( i = 0; i < models.length; i++ ){
				
				camera.getDistance(models[i]);
				
			}
			
			models.sortOn("zDist", Array.DESCENDING);
		}
		
		private function script(e:Event):void {
			timer.reset();
			timer.start();
		}
		
		private function controlIntro(e:TimerEvent):void {
			
			
			//if(timerIntro.currentCount == 5){
			if(camera.Position.z > 20){
				timerIntro.removeEventListener(TimerEvent.TIMER, controlIntro);
				invtroStart = true;
				
			
				value.x = 0;
				value.y = 0;
				value.z = 0;
				
				
				textor.visible = true;
				init();
			}
		}
		
		private function changeTextor(e:TimerEvent):void {
			
			if(timer.currentCount == currentDelay){
				
				
				timer.stop();
				switch(currentSlide){
					case "SLIDE1":
					textor.init(new XML(data.slide2));
					currentSlide = "SLIDE2";
					currentDelay = int(data.slide2.@delay);
					break;
					case "SLIDE2":
					textor.init(new XML(data.slide3));
					currentSlide = "SLIDE3";
					currentDelay = int(data.slide3.@delay);
					break;
					case "SLIDE3":
					textor.init(new XML(data.slide4));
					currentSlide = "SLIDE4";
					currentDelay = int(data.slide4.@delay);
					break;
					case "SLIDE4":
					textor.init(new XML(data.slide5));
					currentSlide = "SLIDE5";
					currentDelay = int(data.slide5.@delay);
					break;
					case "SLIDE5":
					textor.init(new XML(data.slide6));
					currentSlide = "SLIDE6";
					currentDelay = int(data.slide6.@delay);
					break;
					case "SLIDE6":
					textor.init(new XML(data.slide7));
					currentSlide = "SLIDE7";
					currentDelay = int(data.slide7.@delay);
					break;
					case "SLIDE7":
					textor.init(new XML(data.slide8));
					currentSlide = "SLIDE8";
					currentDelay = int(data.slide8.@delay);
					break;
					case "SLIDE8":
					textor.init(new XML(data.slide1));
					currentSlide = "SLIDE1";
					currentDelay = int(data.slide1.@delay);
					break;
				}
			}
		}
	}
}
